package com.epcs.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

public class EpcsDateTimeUtil {

	private static DateTimeFormatter formater = null;

	public static String getIsoDateTime(Long milliseconds) {
		formater = ISODateTimeFormat.dateTimeNoMillis();
		return formater.print(milliseconds);
	}

	public static Long getMillisecondsByString(String dateTime) {
		return DateTime.parse(dateTime).getMillis();
	}

	public static String getCurrentIsoDateTime() {
		formater = ISODateTimeFormat.dateTimeNoMillis();
		DateTime current = new DateTime();
		return formater.print(current);
	}

	public static String getCurrentMothString() {
		int month = Calendar.getInstance().get(Calendar.MONTH) + 1;

		if (month < 10) {
			return "0" + String.valueOf(month);
		}
		return String.valueOf(month);
	}

	public static String getCurrentYearString() {
		return String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
	}

	public static String getCurrentIsoByPattern(String pattern) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(calendar.getTime());
	}

	public static String covertTo2Digit(String number) {
		if (number.length() < 2) {
			return "0" + number;
		}
		return number;
	}

	public static int getCurrentMothInteger() {
		int month = Calendar.getInstance().get(Calendar.MONTH) + 1;

		return month;
	}

	public static int getCurrentYearInteger() {
		return Calendar.getInstance().get(Calendar.YEAR);
	}

	public static String convertToSystemDatetime(String dateTime) {
		formater = ISODateTimeFormat.dateTimeNoMillis();
		DateTime result = DateTime.parse(dateTime);
		return formater.print(result);
	}

	public static String getNextDayISO(String dateTime) {
		formater = ISODateTimeFormat.dateTimeNoMillis();
		DateTime result = DateTime.parse(dateTime);
		result = result.plusDays(1);
		return formater.print(result);
	}
	
	public static String getPreviousDayISO(String dateTime) {
		formater = ISODateTimeFormat.dateTimeNoMillis();
		DateTime result = DateTime.parse(dateTime);
		result = result.minusDays(1);
		return formater.print(result);
	}

	public static String convertToJSPDatetime(String dateTime) {
		Long milisecond = getMillisecondsByString(dateTime);
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(milisecond.longValue());
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
		return format.format(calendar.getTime());
	}
	public static long compareCurrentTimeWithString(String str){
		//Get thời gian hiện tại
		String currentTime = getCurrentIsoByPattern("yyyy-MM-dd'T'HH:mm:ssZ");
		
		//Tạo formatter
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ");
		
		//parse  from string to datetime
		DateTime curTime = formatter.parseDateTime(currentTime);
		DateTime inputDate = formatter.parseDateTime(str);
		long result = (inputDate.getMillis() -  curTime.getMillis())/60000; 
		return result;
	}
	public static String convertDateTimeWithPattern(String inputDateTime, String pattern){
		Long milisecond = getMillisecondsByString(inputDateTime);
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(milisecond.longValue());
		SimpleDateFormat format = new SimpleDateFormat(pattern);//"HH:mm dd-MM-yyy"
		return format.format(calendar.getTime());
	}
	public static boolean checkDateTimeIsTomorrow(String str){
		boolean result=false;
		//Get thời gian hiện tại
		String currentTime = getCurrentIsoByPattern("yyyy-MM-dd'T'HH:mm:ssZ");
		
		//Tạo formatter
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ");
		
		//parse  from string to datetime
		DateTime curTime = formatter.parseDateTime(currentTime);
		DateTime inputDate = formatter.parseDateTime(str);
		if(inputDate.getDayOfYear() - curTime.getDayOfYear() == 1){
			result = true;
		}
		return result;
	}
	
	public static boolean checkDateTimeIsToday(String str){
		boolean result=false;
		//Get thời gian hiện tại
		String currentTime = getCurrentIsoByPattern("yyyy-MM-dd'T'HH:mm:ssZ");
		
		//Tạo formatter
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ");
		
		//parse  from string to datetime
		DateTime curTime = formatter.parseDateTime(currentTime);
		DateTime inputDate = formatter.parseDateTime(str);
		if(inputDate.getDayOfYear() - curTime.getDayOfYear() == 0){
			result = true;
		}
		return result;
	}
	public static long compareTwoDateTimes(String dateTime1, String dateTime2){
		//Tạo formatter
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ");
		
		//parse  from string to datetime
		DateTime date1 = formatter.parseDateTime(dateTime1);
		DateTime date2 = formatter.parseDateTime(dateTime2);
		long result = (date1.getMillis() -  date2.getMillis())/60000; 
		return result;
	}
	
	public static void main(String[] args) {
		long android = 1395446400000l;
		long system = 1395507600000l;
		String androidStr = "2014-03-22T07:00:00+07:00";
		String systemStr = "2014-03-23T00:00:00+07:00";
		
		System.out.println("sys = " +getIsoDateTime(system));
		System.out.println("anddroid = " +getIsoDateTime(android));

		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss dd-MM-yyyy Z");
		formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(system);
		//calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
		
		System.out.println("cal sys = " + formatter.format(calendar.getTime()));
		
		System.out.println("android long = " + EpcsDateTimeUtil.getMillisecondsByString(androidStr));
		System.out.println("sys long = " + EpcsDateTimeUtil.getMillisecondsByString(systemStr));
	}

}
